----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_KeepObjectiveTracker = {}

EA_Window_KeepObjectiveTracker.EMPTY_SIZE = { x=600, y=200 } -- Used to provide a size guide for the layout editor.

EA_Window_KeepObjectiveTracker.NUM_OBJECTIVES = 1
EA_Window_KeepObjectiveTracker.NUM_QUESTS = 1
EA_Window_KeepObjectiveTracker.NUM_CONDITION_COUNTERS = 1

EA_Window_KeepObjectiveTracker.WIDTH = 600

EA_Window_KeepObjectiveTracker.currentData = {}

----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

local CompleteCounterColor      = DefaultColor.TEAL
local IncompleteCounterColor    = DefaultColor.WHITE
local CompleteQuestTitleColor   = CompleteCounterColor 
local IncompleteQuestTitleColor = IncompleteCounterColor

----------------------------------------------------------------
-- Local functions
----------------------------------------------------------------
local function DisplayObjectiveOutOfRangeError(index)
    if (index)
    then
        ERROR(L"Active objective #"..index..L" updated, the Keep Tracker only supports one objective")
    else
        ERROR(L"nil active objective updated, what's that mean?")
    end
end

----------------------------------------------------------------
-- EA_Window_KeepObjectiveTracker Functions
----------------------------------------------------------------
-- OnInitialize Handler
function EA_Window_KeepObjectiveTracker.Initialize()

    LayoutEditor.RegisterWindow( "EA_Window_KeepObjectiveTracker",
                                GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_KEEP_OBJECTIVE_TRACKER_WINDOW_NAME ),
                                GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_KEEP_OBJECTIVE_TRACKER_WINDOW_DESC ),
                                false, false,
                                false, nil,
                                { "topleft", "top", "topright" } )
                                

    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_ADDED,   "EA_Window_KeepObjectiveTracker.OnQuestAdded")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_UPDATED, "EA_Window_KeepObjectiveTracker.OnQuestUpdated")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_REMOVED, "EA_Window_KeepObjectiveTracker.OnQuestRemoved")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_COMPLETED, "EA_Window_KeepObjectiveTracker.OnQuestCompleted")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_FAILED, "EA_Window_KeepObjectiveTracker.OnQuestFailed")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_RESETTING, "EA_Window_KeepObjectiveTracker.OnQuestResetting")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_CHANGED_STATE,   "EA_Window_KeepObjectiveTracker.OnQuestStateChanged")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PUBLIC_QUEST_OPTOUT, "EA_Window_KeepObjectiveTracker.OnQuestOptOut")

    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.INTERFACE_RELOADED,           "EA_Window_KeepObjectiveTracker.Refresh" )
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.PLAYER_ZONE_CHANGED,          "EA_Window_KeepObjectiveTracker.Refresh" )
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.CAMPAIGN_ZONE_UPDATED,        "EA_Window_KeepObjectiveTracker.OnCampaignZoneUpdated")
    WindowRegisterEventHandler( "EA_Window_KeepObjectiveTracker", SystemData.Events.CAMPAIGN_PAIRING_UPDATED,     "EA_Window_KeepObjectiveTracker.OnCampaignPairingUpdated")
    
    LabelSetText("EA_Window_KeepObjectiveTrackerLockedText", GetStringFromTable( "ObjectiveTracker", StringTables.ObjectiveTracker.TEXT_BATTLEFIELD_LOCKEDZONE) )
    
    EA_Window_KeepObjectiveTracker.UpdateFullList()
    
end

-- OnUpdate Handler
function EA_Window_KeepObjectiveTracker.Update( timePassed )
    EA_Window_KeepObjectiveTracker.UpdateTimer( timePassed )
end

-- OnShutdown Handler
function EA_Window_KeepObjectiveTracker.Shutdown()

end

function EA_Window_KeepObjectiveTracker.Refresh()
    EA_Window_KeepObjectiveTracker.UpdateTracker()
end

function EA_Window_KeepObjectiveTracker.OnCampaignZoneUpdated( zoneId )
    if( zoneId == GameData.Player.zone )
    then
        EA_Window_KeepObjectiveTracker.UpdateTracker()
    end
end

function EA_Window_KeepObjectiveTracker.OnCampaignPairingUpdated( pairingId )
    EA_Window_KeepObjectiveTracker.UpdateTracker()
end

----------------------------------------------------------------
-- Quests
----------------------------------------------------------------
function EA_Window_KeepObjectiveTracker.OnQuestAdded() 
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.OnQuestAdded: index="..index )
    
    if (not DataUtils.activeObjectivesData[index].isKeep)
    then
        return
    end

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    -- Add the data
    EA_Window_KeepObjectiveTracker.currentData[index] = { objectiveId=DataUtils.activeObjectivesData[index].id }
    
    -- Update the Window to Include the new objective       
    EA_Window_KeepObjectiveTracker.UpdateTracker( )
    
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_ADDED )
    
end

function EA_Window_KeepObjectiveTracker.OnQuestResetting()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.OnQuestResetting" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    
    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    local showing = WindowGetShowing( "EA_Window_KeepObjectiveTracker" )
    
    if ( showing == false )
    then
    
        -- Sound
        Sound.Play( Sound.PUBLIC_QUEST_CYCLING )

    end
    
end

function EA_Window_KeepObjectiveTracker.OnQuestUpdated() 
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.OnQuestUpdated" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    
    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    -- Update the Window to Include the new objective       
    EA_Window_KeepObjectiveTracker.UpdateTracker( )
end


function EA_Window_KeepObjectiveTracker.OnQuestRemoved()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    
    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.OnQuestRemoved() index = "..index)

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
           
    -- Slide all of the other data up on the window
    for objective = index, EA_Window_KeepObjectiveTracker.NUM_OBJECTIVES - 1
    do
        EA_Window_KeepObjectiveTracker.currentData[objective] = EA_Window_KeepObjectiveTracker.currentData[objective+1]
    end
    EA_Window_KeepObjectiveTracker.currentData[EA_Window_KeepObjectiveTracker.NUM_OBJECTIVES] = nil
    
    -- Update the windows
    EA_Window_KeepObjectiveTracker.UpdateTracker( )
    
end

function EA_Window_KeepObjectiveTracker.OnQuestCompleted()
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.OnQuestCompleted()")
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_COMPLETED )
end

function EA_Window_KeepObjectiveTracker.OnQuestFailed()
    -- DEBUG(L" **EA_Window_KeepObjectiveTracker.OnQuestFailed" )
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_FAILED )
end


function EA_Window_KeepObjectiveTracker.OnQuestStateChanged()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L" **EA_Window_KeepObjectiveTracker.OnQuestStateChanged" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end

    -- Update the Quest Data 
    EA_Window_KeepObjectiveTracker.UpdateTracker( )
end


-- This is only called on initialization to populate the window without any state changing animations
function EA_Window_KeepObjectiveTracker.UpdateFullList()

    -- DEBUG(L"EA_Window_KeepObjectiveTracker.UpdateFullList()")
    --DataUtils.activeObjectivesData = GetActiveObjectivesData()

    for objective = 1, EA_Window_KeepObjectiveTracker.NUM_OBJECTIVES
    do

        local isObjectiveValid = false

        for questIndex = 1, EA_Window_KeepObjectiveTracker.NUM_QUESTS
        do
            local isQuestValid = false

            if ( DataUtils.activeObjectivesData ~= nil )
            then
                if ( DataUtils.activeObjectivesData[objective] ~= nil )
                then
                    if( DataUtils.activeObjectivesData[objective].Quest[questIndex] ~= nil )
                    then
                        isQuestValid = DataUtils.activeObjectivesData[objective].Quest[questIndex].name ~= L"" and
                                       DataUtils.activeObjectivesData[objective].Quest[questIndex].name ~= nil
                    end
                end
            end
    
            WindowSetShowing( "EA_Window_KeepObjectiveTrackerDuringActionQuest", isQuestValid )
            
            isObjectiveValid = isObjectiveValid or isQuestValid
        end
        
        if( isObjectiveValid )
        then
            --DEBUG(L"Battlefielc Objective #"..objective..L" = "..GameData.ActiveObjectives[objective].name )
            --DEBUG(L"  Objective #"..objective..L" has data.")
            EA_Window_KeepObjectiveTracker.currentData[objective] = { objectiveId=DataUtils.activeObjectivesData[objective].id }
            EA_Window_KeepObjectiveTracker.UpdateTracker( )
        else
            --DEBUG(L"  Objective #"..objective..L" has no data.")
            local objectiveWindowString = "EA_Window_KeepObjectiveTracker"
            WindowSetShowing( objectiveWindowString, false )
        end     

    end     
        
end


-- Updates the Quest & Condition Data for an Objective
function EA_Window_KeepObjectiveTracker.UpdateTracker()
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.UpdateTracker()")

    local objective = 1
        
    local objectiveData         = DataUtils.activeObjectivesData[objective]
    local trackerWindow         = "EA_Window_KeepObjectiveTracker"
    local lockedWindow          = "EA_Window_KeepObjectiveTrackerLocked"

    local useKeepTracker = objectiveData ~= nil and
                           objectiveData.isKeep and
                           not GameData.Player.isInScenario
                                  
    WindowSetShowing( trackerWindow, useKeepTracker  )
    
    if ( not useKeepTracker )
    then
        return
    end    

    -- DEBUG(L"  : #"..objectiveData.id..L" '"..objectiveData.name..L"' control = "..GameData.ActiveObjectives[1].curControlPoints )

     -- set opt out flag
    ButtonSetPressedFlag( trackerWindow.."OptOutButton", objectiveData.optedOutForLoot )
    
    local displayData = EA_Window_KeepObjectiveTracker.currentData[objective]
    local objectiveID = objectiveData.id
    
    -- Capture ownership and state
    local iconWindow = trackerWindow.."OwnerIcon"
    local owner = TrackerUtils.GetKeepSliceForOwner(objectiveData.controllingRealm)
    DynamicImageSetTextureSlice(iconWindow, owner)
    
    -- Location name
    local name = DataUtils.activeObjectivesData[objective].name
    LabelSetText(trackerWindow.."Label", name )
    
    -- Check for zone lock.
    local zoneData = GetCampaignZoneData( GameData.Player.zone )    
    if( (zoneData ~= nil) and (zoneData.isLocked) )
    then
        local pairingData = GetCampaignPairingData( zoneData.pairingId )
        if( pairingData.contestedZone ~= 0  )
        then  
            -- The Battle is in annother zone in this pairing or in the city
            local frontLineZoneName = GetZoneName(pairingData.contestedZone)
            local frontLineText = GetStringFormatFromTable( "ObjectiveTracker", StringTables.ObjectiveTracker.TEXT_BATTLEFIELD_FRONTZONE, {frontLineZoneName} )
            LabelSetText( lockedWindow.."Location", frontLineText )
        else
            local capturedRealmName = ( GetRealmName(pairingData.controllingRealm) )
                        
            local capturedFortressName = L""
            if( pairingData.controllingRealm == GameData.Realm.DESTRUCTION)
            then
                capturedFortressName = GetZoneName(pairingData.orderFortressZone) 
            elseif( pairingData.controllingRealm == GameData.Realm.ORDER)
            then
                capturedFortressName = GetZoneName(pairingData.destructionFortressZone) 
            end
            
            
            local capturedText = GetStringFormatFromTable( "ObjectiveTracker", StringTables.ObjectiveTracker.TEXT_BATTLEFIELD_CAPTURED, { capturedRealmName, capturedFortressName } )
            LabelSetText( lockedWindow.."Location", capturedText )
        end
        
        WindowSetShowing( "EA_Window_KeepObjectiveTrackerOptOutButton", false )
        WindowSetShowing( lockedWindow,          true )
        return
    else
        WindowSetShowing( "EA_Window_KeepObjectiveTrackerOptOutButton", true )
        WindowSetShowing( lockedWindow,          false )
    end
    
    
    -- Quests
    for questIndex = 1, EA_Window_KeepObjectiveTracker.NUM_QUESTS
    do
        -- DEBUG(L"Made it through 02")
        local questData = objectiveData.Quest[questIndex]
        local questWindowString = "EA_Window_KeepObjectiveTrackerDuringActionQuest"
        
        if ( questData ~= nil and DataUtils.activeObjectivesData[GameData.ActiveObjectives.updatedObjectiveIndex].isKeep )
        then

            local questName = questData.name
            LabelSetText( questWindowString, questName )            
            LabelSetTextColor(questWindowString, CompleteCounterColor.r, CompleteCounterColor.g, CompleteCounterColor.b)
        
            -- DEBUG(L"Made it through 03")    
            
            WindowSetShowing( questWindowString, true )
            --DEBUG(L"  Quest #"..questIndex..L" '"..GameData.ActiveObjectives[objective].Quest[questIndex].name..L"'")
            
            -- Quest name            
            local questName = DataUtils.activeObjectivesData[objective].Quest[questIndex].name

            -- Conditions
            for index, data in ipairs(questData.conditions)
            do
                local conditionName = data.name
                local nameLabel     = questWindowString

                -- Only show the conditions if conditionName is not empty
                if( conditionName ~= L"" )
                then
                    LabelSetText( nameLabel, conditionName )            
                    LabelSetTextColor(nameLabel, IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                end
            end
            
        else
            WindowSetShowing(questWindowString, false)
        end
        
    end        
    -- DEBUG(L"Made it through 07 - exiting")

end

function EA_Window_KeepObjectiveTracker.UpdateTimer( timePassed )
    local timerContainerWindow = "EA_Window_KeepObjectiveTrackerDuringAction"
    
    for objectiveIndex = 1, EA_Window_KeepObjectiveTracker.NUM_OBJECTIVES
    do
        for questIndex = 1, EA_Window_KeepObjectiveTracker.NUM_QUESTS
        do
            if (DataUtils.activeObjectivesData == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex] == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex].Quest == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex] == nil)
            then
                LabelSetText( timerContainerWindow.."TimerValue", L"" )      
                WindowSetShowing( timerContainerWindow.."ClockImage", false )
            else
                -- Timer (only show timer at all if this is the first quest in the first objective)
                if( DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].maxTimer ~= 0 and
                    DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].maxTimer ~= nil )
                then
                    -- DEBUG(L"Made it through 05 - timer")
                    local timeLeft = DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].timeLeft
                    local text = TimeUtils.FormatClock(timeLeft)
                    LabelSetText( timerContainerWindow.."TimerValue", text )
                    WindowSetShowing( timerContainerWindow.."ClockImage", true )
                else            
                    LabelSetText( timerContainerWindow.."TimerValue", L"" )      
                    WindowSetShowing( timerContainerWindow.."ClockImage", false )
                end
            end
        end
    end
end


----------------------------------------------------------------
-- Tooltips
----------------------------------------------------------------

function EA_Window_KeepObjectiveTracker.MouseOverQuest()

    local objective  = 1
    local questIndex = 1
    -- DEBUG(L"EA_Window_KeepObjectiveTracker.MouseOverQuest - ActiveObjective Index = "..objective..L", quest index = "..questIndex )

    if (DataUtils.activeObjectivesData[objective] == nil)
    then
        return
    elseif ( DataUtils.activeObjectivesData[objective].Quest[questIndex] == nil )
    then
        return
    end
    
    local objectiveData = DataUtils.activeObjectivesData[objective]
    local questData     = objectiveData.Quest[questIndex]

    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name )    
    local row = 1
    local column = 1

    -- Name
    local text = questData.name
    Tooltips.SetTooltipFont( row, column, "font_default_sub_heading", WindowUtils.FONT_DEFAULT_SUB_HEADING_LINESPACING  )
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, 255, 204, 102 )
    row = row + 1
    
    -- Text
    local text = questData.desc
    Tooltips.SetTooltipText( row, column, text )
    row = row + 1
    column = 1
    

    Tooltips.Finalize()
    Tooltips.AnchorTooltip( Tooltips.ANCHOR_WINDOW_LEFT )   
end

function EA_Window_KeepObjectiveTracker.ToggleOptOut()
    
    local optedOutForLootRoll = ButtonGetPressedFlag( "EA_Window_KeepObjectiveTrackerOptOutButton" )
    TrackerUtils.OnMouseOverOptOut()
    
    local objective = 1    
    if( DataUtils.activeObjectivesData[objective] )
    then
        local objectiveId = DataUtils.activeObjectivesData[objective].id
        LootRollOptOut(objectiveId, optedOutForLootRoll) 
    end
    
end

function EA_Window_KeepObjectiveTracker.OnQuestOptOut(index, optOut)

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    ButtonSetPressedFlag( "EA_Window_KeepObjectiveTrackerOptOutButton", optOut )

    DataUtils.activeObjectivesData[index].optedOutForLoot = optOut
end

